<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="编号" prop="id">
        <el-input v-model="queryParams.id"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择">
          <el-option label="初始" value="INIT" />
          <el-option label="完成" value="SUCCESS" />
          <el-option label="失败" value="FAIL" />
          <el-option label="处理中" value="ING" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="walletRechargeCashList" @selection-change="handleSelectionChange">
      <el-table-column label="编号" align="center" prop="id" show-overflow-tooltip />
      <el-table-column label="金额" align="center" prop="amount" width="80px" />
      <el-table-column label="渠道" align="center" prop="channel" />
      <el-table-column label="状态" align="center" prop="statusLabel">
        <template #default="scope">
          <el-tag v-if="scope.row.status === '0'" type="info">{{ scope.row.statusLabel }}</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="success">{{ scope.row.statusLabel }}</el-tag>
          <el-tag v-else-if="scope.row.status === '2'" type="danger">{{ scope.row.statusLabel }}</el-tag>
          <el-tag v-else>{{ scope.row.statusLabel }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="充值图片" align="center" prop="imgUrl" min-width="100px">
        <template #default="scope">
          <el-image class="czimg" fit="fill" :src="scope.row.imgUrl" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.current" v-model:limit="queryParams.size"
      @pagination="getList" />

    <!-- 驳回订单填入原因对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <template #footer>
        <el-form-item label="驳回理由">
          <el-input v-model="refuseForm.refuseReason"></el-input>
        </el-form-item>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitRefuseForm">确 定</el-button>
          <el-button>取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog title="新增充值" v-model="addShow" width="1100px" append-to-body>
      <el-form label-width="80px">
        <el-form-item label="充值方式">
          <el-radio-group v-model="addForm.channel">
            <el-radio :label="`USDT`" size="large">USDT</el-radio>
            <el-radio :label="`INR`" size="large">INR</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="充值渠道">
          <el-image :src="qr"></el-image>
          <div style="word-wrap: break-word;">{{ addForm.cashAddress }}</div>
        </el-form-item>
        <div style="height: 20px"></div>
        <el-form-item label="USDT充值" v-if="addForm.channel == 'USDT'">
          <el-radio-group v-model="addForm.amount">
            <el-radio :label="42375" size="large">
              <div>充值金额: 500</div>
              <div>汇率: 41800</div>
              <div>利润: 575</div>
              <div>实际到账: 42375</div>
            </el-radio>
            <el-radio :label="84770" size="large">
              <div>充值金额: {{1000}}</div>
              <div>汇率: {{83600}}</div>
              <div>利润: {{1170}}</div>
              <div>实际到账: {{84770}}</div>
            </el-radio>
            <el-radio :label="84770 *3" size="large">
              <div>充值金额: 3000</div>
              <div>汇率: {{ 83600*3 }}</div>
              <div>利润: {{ 1170*3 }}</div>
              <div>实际到账: {{84770 *3}}</div>
            </el-radio>
            <el-radio :label="4770 *5" size="large">
              <div>充值金额: {{1000 *5}}</div>
              <div>汇率: {{83600 *5}}</div>
              <div>利润: {{1170 *5}}</div>
              <div>实际到账: {{84770 *5}}</div>
            </el-radio>
            <el-radio :label="847700" size="large">
              <div>充值金额: {{10000}}</div>
              <div>汇率: {{836000}}</div>
              <div>利润: {{11700}}</div>
              <div>实际到账: {{847700}}</div>
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="INR充值" v-if="addForm.channel == 'INR'">
          <el-radio-group v-model="addForm.amount">
            <el-radio :label="41800" size="large">
              <div>充值金额: {{41800}}</div>
              <div>利润: {{575}}</div>
              <div>实际到账: {{42375}}</div>
            </el-radio>
            <el-radio :label="83600" size="large">
              <div>充值金额: {{41800*2}}</div>
              <div>利润: {{575*2}}</div>
              <div>实际到账: {{42375*2}}</div>
            </el-radio>
            <el-radio :label="250800" size="large">
              <div>充值金额: {{250800}}</div>
              <div>利润: {{3500}}</div>
              <div>实际到账: {{254300}}</div>
            </el-radio>
            <el-radio :label="418000" size="large">
              <div>充值金额: {{418000}}</div>
              <div>利润: {{6000}}</div>
              <div>实际到账: {{424000}}</div>
            </el-radio>
            <el-radio :label="836000" size="large">
              <div>充值金额: {{836000}}</div>
              <div>利润: {{12000}}</div>
              <div>实际到账: {{848000}}</div>
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <div style="height: 30px;"></div>
        <el-form-item label="上传凭证">
          <image-upload v-model="addForm.imgUrl" :limit="1"></image-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="addSubmit">确 定</el-button>
          <el-button @click="addCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WalletRechargeCash">
import { confirmScore, refuseScore } from "@/api/boot/walletRechargeCash";
import { walletTransList, createRecharge, getWalletCode, confirmRecharge, refuseRecharge} from "@/api/charge/index.js";
import qrcode from 'qrcode'

const { proxy } = getCurrentInstance();

const walletRechargeCashList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const addShow = ref(false)
const total = ref(0);
const title = ref("");
const qr = ref('')
const qrUrl = ref('')
const addForm = ref({
  channel: 'USDT',
  amount: '',
  imgUrl: '',
  cashAddress: '',
  type: `1`
})
const refuseForm = ref({
  id: '',
  refuseReason: ''
})
const data = reactive({
  form: {},
  queryParams: {
    current: 1,
    size: 10,
    type: 1
  },
  rules: {
    
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询充值提现流水列表 */
function getList() {
  loading.value = true;
  walletTransList(queryParams.value).then(res => {
    walletRechargeCashList.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  })
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
//新增充值
function handleAdd() {
  addShow.value = true
}
function addSubmit() {
  createRecharge(addForm.value).then(res => {
    if(res.code == 200) {
      addForm.value.imgUrl = ''
      addShow.value = false
      proxy.$modal.msgSuccess("添加成功");
      getList();
    }
  })
}
function addCancel() {
  addShow.value = false
}
// 表单重置
function reset(row) {
  form.value = {
    id: row.id
  };
  proxy.resetForm("walletRechargeCashRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.current = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

function handleConfirm(row) {
  proxy.$modal.confirm_tg('是否确认【' + row.type + '】订单"' + row.id + '"完成', '确认订单').then(function () {
    confirmRecharge(row.id).then(res => {
      if (res.code === 200) {
        proxy.$modal.msgSuccess("操作成功");
        getList();
      }
    })
  }).catch(() => { })
}

function handleRefuse(row) {
  refuseForm.value.id = row.id
  title.value = '驳回'
  open.value = true;
}

function submitRefuseForm() {
  refuseRecharge(refuseForm.value).then(res => {
    if (res.code === 200) {
      proxy.$modal.msgSuccess("驳回成功");
      open.value = false;
      getList();
    }
  })
}
function getCode() {
  getWalletCode().then(res => {
    let QRData = res.data.walletAddress
    addForm.value.cashAddress = res.data.walletAddress
    qrUrl.value = res.data.walletAddress
    qrcode.toDataURL(QRData, (err, url) => {
      if(err) {
        
      } else {
        qr.value = url
      }
    })
  })
}
getList();
getCode();
</script>
<style>
.czimg {
  width: 30px;
  height: 30px;
}

.czimg:hover {
  width: 100px;
  height: 100px;
}
</style>